<template>
    <rui-page :defines="defines">
        <div style="background-color: #f9fafc;">
                <Alert show-icon style="height: 30px;">
                    <div style="margin-top:-2px" >请输入查询条件</div>
                </Alert>
                <rui-query
                           ref="searchForm"
                           :query="queryData"
                           :query-rows="searchFormOptions"
                />
        </div>
        <div style="background-color: #f9fafc;">

            <rui-table
                :defineId="'creditTemplate'"
                :fixedLeft="['opt']"
                :showIndex="false"
                :select="false"
                :columns="creditColumns"
                :slots="[{key:'opt',slot:'opt'}]"
                @loadDatas="queryData"
                ref-query="searchForm"
                ref="creditTable"
                >
                <template slot="toolBar">
                        <Button @click="add" type="primary" icon="md-add">新增</Button>
                </template>
                <template slot="opt" slot-scope="{ row }">
                    <div>
                        <Button @click="edit(row)" size="small" type="primary" icon="ios-create">编辑</Button>
                        <Button type="error" size="small" @click="remove(row)" icon="md-trash">删除</Button>
                    </div>
                </template>
            </rui-table>
        </div>
            <Modal
                v-model="templateModalVisible"
                :title="modalTitle"
                :closable="false"
                :width="800"
                :mask-closable="false"
                @on-ok="templateSubmit"
            >
                <rui-form :form-options="templateFormOptions" :read-only="false"
                          ref="templateForm" style="margin-left:18%;margin-top:-30px;height: 200px;"></rui-form>
                <div slot="footer">
                    <Button size="small" type="primary"  @click="templateModalVisible = false" icon="ios-backspace">取消</Button>
                    <Button type="primary" size="small" @click="templateSubmit" icon="md-bookmark">保存</Button>
                </div>
            </Modal>
    </rui-page>
</template>
<script>
    import creditTemplateDefine from "../defines/credit-template-define";
    import {
        getTemplateList,
        addTemplate,
        editTemplate,
        deleteTemplate
    } from "@/projects/afs-core-business/api/afs-case/credit-template/creditTemplate";
    import {deepClone} from "@/libs/utils/ObjectClone";
    //调取基础库api所有角色信息
    import {
        getAllRoleList
    } from "@/projects/basic/api/admin/role";
    export default {
        name: "credit-template",
        data() {
            return {
                templateModalVisible: false,
                modalTitle: "新增模板",
                searchFormOptions: [
                             {
                                 defineId: "creditTemplate", span:6, fields: ["userName","templateName"]}
                                 ],
                templateFormOptions: [
                    {
                        grids: [
                            {
                                defineId: "creditTemplate",
                                span: 16,
                                fields: ["templateName", "templateContent"]
                            },
                        ]

                    }
                ],
                creditColumns: [
                    'opt', 'templateName', 'templateContent','userName','createTime'
                ],
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "creditTemplate",
                        fields: creditTemplateDefine
                    }
                ]
            }
        },
        methods: {
            queryData(queryData) {
                getTemplateList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.creditTable.updateTableData(records, total);
                    }
                });
            },
            refresh() {
                this.$refs['creditTable'].reloadData();
            },
            templateSubmit() {
                this.$refs.templateForm.getForm().validate(valid => {
                    if (valid) {
                        if (this.modalType === 0) {
                            addTemplate(this.$refs.templateForm.getFormData())
                                .then(res => {
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.refresh();
                                        this.templateModalVisible = false;
                                    }
                                })
                            .catch(() => {
                                });
                        } else {
                            // 编辑
                            editTemplate(this.$refs.templateForm.getFormData())
                                .then(res => {
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.templateModalVisible = false;
                                        this.refresh();
                                    }
                                    if (res.code === "0001") {
                                        this.$Message.success(res.msg);
                                    }
                                })
                                .catch(() => {
                                    this.refresh();
                                });
                        }
                    }
                });
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除模板: " + v.templateName + " ?",
                    onOk: () => {
                        deleteTemplate(v.id).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.refresh();
                            }
                        });
                    }
                });
            },

            add() {
                this.$refs.templateForm.resetFrom();
                this.modalType = 0;
                this.modalTitle = "新增";
                this.templateModalVisible = true;
                this.$refs.templateForm.setAllFieldRequired(true);
            },
            edit(row) {
                this.modalType = 1;
                this.modalTitle = "编辑";
                this.$refs.templateForm.updateFormData(row);
                this.$refs.templateForm.setAllFieldRequired(true);
                this.templateModalVisible = true;
            },
        },
        mounted() {
            this.refresh();
        }
    };
</script>
